/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.List;

import frames.WriteGroupsToXMLwithFrames;
import getData.GetDataFromFieldsOfFramesXML;

/**
 *
 * @author KAS
 */
public class DetailFrame extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2358023179042454292L;
	private static String nameOfXMLForFrame;
	private static String nameOfFrameWhereWeWillBeManagingFields;
	private static String nameOfFieldWhichPropertiesWeWillBeChange;
	private static List<String> listOfAllNamesOfFieldsInThisFrame;
	/**
     * Creates new form DetailFrame
     */
    public DetailFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        new javax.swing.JSeparator();
        jTextFieldNameOfFrame = new javax.swing.JTextField();
        jLabelNameOfField = new javax.swing.JLabel();
        jLabelAppointment = new javax.swing.JLabel();
        jCheckBoxIsEditable = new javax.swing.JCheckBox();
        jCheckBoxUnique = new javax.swing.JCheckBox();
        jCheckBoxCouldBeNull = new javax.swing.JCheckBox();
        jLabelMainLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaAppointment = new javax.swing.JTextArea();
        jLabelFieldType = new javax.swing.JLabel();
        jComboBoxFieldType = new javax.swing.JComboBox();
        jLabelHowToFill = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaHowtoFill = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaComment = new javax.swing.JTextArea();
        FieldComment = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setTitle(nameOfFieldWhichPropertiesWeWillBeChange);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextFieldNameOfFrame.setText("Field name");

        jLabelNameOfField.setText("Name of current field");

        jLabelAppointment.setText("Appointment");

        jCheckBoxIsEditable.setText("Editable");
        //jCheckBoxIsEditable.setToolTipText("");
        selectedValueForJComboBoxFieldType = getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Editable");
        checkBooleanForTrueOrFalse();
        jCheckBoxIsEditable.setSelected(findOutIfCheckboxTrueOrFalse);
 
        jCheckBoxUnique.setText("Unique at this project");
        //jCheckBoxUnique.setToolTipText("");
        selectedValueForJComboBoxFieldType = getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Unique");
        checkBooleanForTrueOrFalse();
        jCheckBoxUnique.setSelected(findOutIfCheckboxTrueOrFalse);
 
        jCheckBoxCouldBeNull.setText("Could be without value");
        //jCheckBoxCouldBeNull.setToolTipText("");
        selectedValueForJComboBoxFieldType = getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Could be null");
        checkBooleanForTrueOrFalse();
        jCheckBoxCouldBeNull.setSelected(findOutIfCheckboxTrueOrFalse);
        
        jLabelMainLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelMainLabel.setText("Datails of current field in frame:");

        jTextAreaAppointment.setColumns(20);
        jTextAreaAppointment.setRows(5);
        jTextAreaAppointment.setText(getDataFromFieldsOfFramesXML.getValueForField
		(nameOfXMLForFrame, numberOfRowInLine, "Appointment"));
        jScrollPane1.setViewportView(jTextAreaAppointment);

        jLabelFieldType.setText("Data type");

        jComboBoxFieldType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "text", "int", "date", "link", "button" }));
        String selectedValueForJComboBoxFieldType = getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Field type");
        selectedValueForJComboBoxFieldType = selectedValueForJComboBoxFieldType.replaceAll("[\\ \\-\\+\\.\\^:,]","");
        //System.out.println("selectedValueForJComboBoxFieldType " + selectedValueForJComboBoxFieldType);
        int selectedIndexForJComboBoxFieldType = -1;
        if(selectedValueForJComboBoxFieldType.equals("text")){selectedIndexForJComboBoxFieldType = 0;}
        if(selectedValueForJComboBoxFieldType.equals("int")){selectedIndexForJComboBoxFieldType = 1;}
        if(selectedValueForJComboBoxFieldType.equals("date")){selectedIndexForJComboBoxFieldType = 2;}
        if(selectedValueForJComboBoxFieldType.equals("link")){selectedIndexForJComboBoxFieldType = 3;}
        if(selectedValueForJComboBoxFieldType.equals("button")){selectedIndexForJComboBoxFieldType = 4;}
        //System.out.println("selectedValueForJComboBoxFieldType: " + selectedValueForJComboBoxFieldType);
        //System.out.println("selectedIndexForJComboBoxFieldType " + selectedIndexForJComboBoxFieldType);
        jComboBoxFieldType.setSelectedIndex(selectedIndexForJComboBoxFieldType);
        
        jLabelHowToFill.setText("How to fill");
        
        //jTextFieldNameOfFrame.setColumns(20);
        //jTextFieldNameOfFrame.setRows(5);
        jTextFieldNameOfFrame.setText(getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Name of field"));
       // jScrollPane2.setViewportView(jTextAreaHowtoFill);
        
        
        jTextAreaHowtoFill.setColumns(20);
        jTextAreaHowtoFill.setRows(5);
        jTextAreaHowtoFill.setText(getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "How to fill"));
        jScrollPane2.setViewportView(jTextAreaHowtoFill);

        jTextAreaComment.setColumns(20);
        jTextAreaComment.setRows(5);
        jTextAreaComment.setText(getDataFromFieldsOfFramesXML.getValueForField
        		(nameOfXMLForFrame, numberOfRowInLine, "Comment"));
        jScrollPane3.setViewportView(jTextAreaComment);

        FieldComment.setText("Comment");
        
        jButtonSave.setText("Save");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelMainLabel)
                    .addComponent(jLabelNameOfField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                    .addComponent(jComboBoxFieldType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1)
                    .addComponent(jTextFieldNameOfFrame))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jButtonSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jSeparator3)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(FieldComment))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelFieldType))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelHowToFill))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jCheckBoxCouldBeNull))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jCheckBoxUnique))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jCheckBoxIsEditable))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelAppointment)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabelMainLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldNameOfFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNameOfField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelAppointment)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxIsEditable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxUnique)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxCouldBeNull)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelHowToFill))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxFieldType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFieldType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(FieldComment))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonCancel))
                .addGap(13, 13, 13))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * 
	 */
	private void checkBooleanForTrueOrFalse() {
		findOutIfCheckboxTrueOrFalse = false;
        selectedValueForJComboBoxFieldType = selectedValueForJComboBoxFieldType.replaceAll("[\\ \\-\\+\\.\\^:,]","");
        if(selectedValueForJComboBoxFieldType.equals("true")){findOutIfCheckboxTrueOrFalse = true;}
    //    if(selectedValueForJComboBoxFieldType.equals("false")){selectedIndexForJComboBoxFieldType = 1;}
	}

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
    	WriteGroupsToXMLwithFrames writeGroupsToXMLwithFrames = new WriteGroupsToXMLwithFrames();
    	String NameOfField = jTextFieldNameOfFrame.getText();
    	String Appointment = jTextAreaComment.getText();
    	String Editable = null;
    	if (jCheckBoxIsEditable.isSelected()){Editable = "true";} else {Editable = "false";}
    	String Unique = null;
    	if (jCheckBoxUnique.isSelected()){Unique = "true";} else {Unique = "false";}
    	String CouldBeNull = null;
    	if (jCheckBoxCouldBeNull.isSelected()){CouldBeNull = "true";} else {CouldBeNull = "false";}
    	String HowToFill = jTextAreaHowtoFill.getText();
    	int fieldType = jComboBoxFieldType.getSelectedIndex();
    	String fieldTypeString = null;
    	if (fieldType == 0) {fieldTypeString = "text";}
    	if (fieldType == 1) {fieldTypeString = "int";}
    	if (fieldType == 2) {fieldTypeString = "date";}
    	if (fieldType == 3) {fieldTypeString = "link";}
    	if (fieldType == 4) {fieldTypeString = "button";}
    	    	
    	String Comment = jTextAreaComment.getText();
    	
    	//System.out.println("FieldType " + fieldTypeString);
    	//    	
    	writeGroupsToXMLwithFrames.rewriteXMLlistOfFramesWithEditedFields(
        		nameOfXMLForFrame, numberOfRowInLine, nameOfFrameWhereWeWillBeManagingFields,
        		NameOfField, Appointment, Editable, Unique, CouldBeNull, HowToFill, fieldTypeString, Comment	
    			);
    			
        jButtonCancelActionPerformed(evt);
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
    	setVisible(false); 
    	dispose(); // not sure
    	FieldsInFrames newFrame = new FieldsInFrames();
    	newFrame.setVisible(true);
    	//newFrame.main(null);
    }//GEN-LAST:event_jButtonCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
    	
    	getDataFromFieldsOfFramesXML = new GetDataFromFieldsOfFramesXML();
        	
    	nameOfXMLForFrame = "src/framesXML/1. This is my first project.xml";
    	nameOfFrameWhereWeWillBeManagingFields = "1. This is my first project";
    	nameOfFieldWhichPropertiesWeWillBeChange = "This is my first field";
    	String nameOfProjectWhereThisFramePlased = "1. This is my first project";
    	String listOfProjectsFileName = "projects.xml";
    	
    	listOfAllNamesOfFieldsInThisFrame = 
    				getDataFromFieldsOfFramesXML.getListOfpropertiesForCurrentField
    						(nameOfXMLForFrame, nameOfFieldWhichPropertiesWeWillBeChange);
    	listOfAllNamesOfFieldsInThisFrame.toArray(new String[listOfAllNamesOfFieldsInThisFrame.size()]);
    	
    	getNumberOfRowInLineForNextWork(); // get numberOfRowInLine
    	//data for appointment received
    	getDataFromFieldsOfFramesXML.getValueForField
			(nameOfXMLForFrame, numberOfRowInLine, "Appointment");
    	   	
    	
    	
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DetailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DetailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DetailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DetailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DetailFrame().setVisible(true);
            }
        });
    }

	private static void getNumberOfRowInLineForNextWork() {
		numberOfRowInLine = 0;
    	int i = 0;
    	for (String string : listOfAllNamesOfFieldsInThisFrame) {
    		System.out.println(nameOfFieldWhichPropertiesWeWillBeChange + " = " + string);
    		if (string.equals(nameOfFieldWhichPropertiesWeWillBeChange)){
    			numberOfRowInLine = i;
    			break;
   			}
    		i++;
     	}
    //	System.out.println(numberOfRowInLine);
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel FieldComment;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JCheckBox jCheckBoxCouldBeNull;
    private javax.swing.JCheckBox jCheckBoxIsEditable;
    private javax.swing.JCheckBox jCheckBoxUnique;
    @SuppressWarnings("rawtypes")
	private javax.swing.JComboBox jComboBoxFieldType;
    private javax.swing.JLabel jLabelAppointment;
    private javax.swing.JLabel jLabelFieldType;
    private javax.swing.JLabel jLabelHowToFill;
    private javax.swing.JLabel jLabelMainLabel;
    private javax.swing.JLabel jLabelNameOfField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextArea jTextAreaAppointment;
    private javax.swing.JTextArea jTextAreaComment;
    private javax.swing.JTextArea jTextAreaHowtoFill;
    private javax.swing.JTextField jTextFieldNameOfFrame;
    // End of variables declaration//GEN-END:variables
	private static int numberOfRowInLine;
	private static GetDataFromFieldsOfFramesXML getDataFromFieldsOfFramesXML;
	private String selectedValueForJComboBoxFieldType;
	private boolean findOutIfCheckboxTrueOrFalse;
}
